/*
 * Photostudio
 */

$(document).ready(function(){
});



/* *********** ALBUM *************** */
var albums = [];

function initAlbum(area, album){
	albums[area] = album;
}

function submitAlbumForm(area, form){
	if ( !CheckMandatoryFields($(form).attr('name')) )
		return false;

	var res = $(form).ajaxSubmit({
		url:'/common/photostudio.php?area='+area+'&mode=albums',
		success:function(data){
			var response = (typeof data == 'undefined' || data.length==0) ? {} : eval( "(" + data + ")" );
			if (response.action == 'add' && typeof response.id!='undefined'){
				albums[area]['rec'+response.id] = {};
				for (var key in response) {
					if ( key!='action' ){
						albums[area]['rec'+response.id][key] = response[key];
					}
				}
			}
			if (response.action == 'edit' && typeof response.id!='undefined'){
				for (var key in response) {
					if ( key!='action' && key!='id' ){
						albums[area]['rec'+response.id][key] = response[key];
					}
				}
			}
			if (response.action == 'del' && typeof response.id!='undefined'){
				delete albums[area]['rec'+response.id];
			}
			refreshAlbumList(area);
			//$.simpleDialog.close;
			$("#"+response.action+"Close"+area).click();
		}
	}); 
	return false;
}

function refreshAlbumList(area){
	var out = '';
	for (var key in albums[area]) {
		out += createAlbumItem(area, albums[area][key].id);
	}
	$("#div_albums"+area).html(out.length ? out : Dictionary.translate('lang_photostudio_albums_no_create'));
}

function createAlbumItem(area, id){
	var item = albums[area]["rec" + id.toString()];
	var out = '';
	if (typeof item != 'undefined'){
		out += '<div class="photo-album-list-item" id="div_album'+item.id+'_'+area+'">';
    out += '<div class="date-nick">';
    out += (item.added_date ? item.added_date : '');
    out += (item.author_nick ? ', '+(item.author_url ? '<a href="'+item.author_url+'">' : '')+item.author_nick+(item.author_url ? '</a>' : '') : '');
    out += '</div>';
    out += '<div class="actions">';
		out += (item.isRightEdit ? '<a href="#" onClick="return editAlbumClick('+area+','+id+')">'+Dictionary.translate('lang_photostudio_album_change')+'</a>' : '');
		out += (item.isRightDel ? '<a href="#" onClick="return delAlbumClick('+area+','+id+')">'+Dictionary.translate('lang_photostudio_album_delete')+'</a>' : '');
    out += '</div>';
    out += '<div class="img-title clearfix">';
		out += (item.img ? (item.url ? '<a href="'+item.url+'" title="'+item.comment+'">' : '')+'<img src="'+item.img+'" />'+(item.url ? '</a>' : '') : '');
    out += (item.url ? '<h3><a href="'+item.url+'" title="'+item.comment+'">' : '')+item.title+(item.url ? '</a></h3>' : '');
    out += (item.num_photos ? item.num_photos : '0') + ' '+Dictionary.translate('lang_photostudio_item_photo');
    out += '</div>';
		out += '</div>';
	}
	return out;
}

function editAlbumClick(area, id){
	var item = albums[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_edit_ID"+area).val(id);
		$("#id_edit_TITLE"+area).val(item.title);
		$("#id_edit_COMMENT"+area).val(item.comment);
		$("a[rel='edit"+area+"']").click();
	}
	return false;
}

function delAlbumClick(area, id){
	var item = albums[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_del_ID"+area).val(id);
		$("a[rel='del"+area+"']").click();
	}
	return false;
}



/* *********** PHOTO *************** */
var photos = [];
var photoArray = [];
var photoCurrent = [];

function initPhoto(area, photo, photoA){
	photos[area] = photo;
	photoArray[area] = photoA;
	if ( document.location.hash.length > 1 ){
		seePhotoClick(area, document.location.hash.substring(1), true);
	}

}

function submitPhotoForm(area, form){
	if ( !CheckMandatoryFields($(form).attr('name')) )
		return false;

	var action = $(':input[name="action'+area+'"]', form).val();
	if ( action=='add' )
	{
		$("#loading"+area).ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});
		var res = $.ajaxFileUpload({
			url: '/common/photostudio.php?area='+area+'&mode=photos&'+$(form).serialize(),
			secureuri: false,
			fileElementId: 'id_add_IMG'+area,
			dataType: 'json',
			success: function (data, status){
				var response = data ;
				if (response.action == 'add' && typeof response.id!='undefined'){
					photos[area]['rec'+response.id] = {};
					for (var key in response) {
						if ( key=='id' || key=='title' || key=='comment' || key=='url' || key=='img_small' || key=='img' || key=='added_date' || key=='author_url' || key=='view_counter' || key=='author_nick' || key=='is_album_title' || key=='isRightEdit' || key=='isRightDel' ){
							photos[area]['rec'+response.id][key] = response[key];
						}
						photos[area]['rec'+response.id]['object'] = response['work_object'];
					}
					refreshPhotoList(area);
					$("#addClose"+area).click();
				}else{
					$("#addClose"+area).click();
					$("#aImgError"+area).click();
				}
			},
			error: function (data, status, e){
				//alert(e);
			}
		});
	}
	else if ( action=='addAvatar' )
	{
		$("#loading"+area).ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});
		var res = $.ajaxFileUpload({
			url: '/common/photostudio.php?area='+area+'&mode=photos&'+$(form).serialize(),
			secureuri: false,
			fileElementId: 'id_add_IMG'+area,
			dataType: 'json',
			success: function (data, status){
				var response = data;
				if (response.action == 'addAvatar' && typeof response.img!='undefined'){
					$("#id_see_img"+area+"").attr('src',response.img);
					$("#id_saveAvatar_ID"+area).val(response.id);
					$("#addAvatarClose"+area).click();
					$("#aImgAvatar"+area).click();
				}else{
					$("#addAvatarClose"+area).click();
					$("#aImgAvatarError"+area).click();
				}
			},
			error: function (data, status, e){
				//alert(e);
			}
		});
	}
	else
	{
		var res = $(form).ajaxSubmit({
			url:'/common/photostudio.php?area='+area+'&mode=photos',
			success:function(data){
				var response = (typeof data == 'undefined' || data.length==0) ? {} : eval( "(" + data + ")" );
				if (response.action == 'edit' && typeof response.id!='undefined'){
					for (var key in response) {
						if ( key!='action' && key!='id' ){
							photos[area]['rec'+response.id][key] = response[key];
						}
					}
				}
				if (response.action == 'albumTitle' && typeof response.id!='undefined'){
					for (var key in photos[area]) {
						photos[area][key].is_album_title = (key=='rec'+response.id) ? '1' : '0';
					}
				}
				if (response.action == 'del' && typeof response.id!='undefined'){
					delete photos[area]['rec'+response.id];
				}
				if (response.action == 'saveAvatar'){
					if ( typeof response.img != 'undefined' && typeof $('#avatarPic') != 'undefined' )
						$('#avatarPic').attr('src', response.img);
					onClickCancelAvatar(area);
				}
				if (response.action == 'edit' || response.action == 'albumTitle' || response.action == 'del'){
					refreshPhotoList(area);
					$("#"+response.action+"Close"+area).click();
				}
			}
		});
	}
	return false;
} 
function refreshPhotoList(area){
	var out = '';
	for (var key in photos[area]) {
		out += createPhotoItem(area, photos[area][key].id);
	}
	$("#div_photos"+area).html(out.length ? out : Dictionary.translate('lang_photostudio_no_photo_in_album'));
}

function createPhotoItem(area, id){
	var item = photos[area]["rec" + id.toString()];
	var out = '';
	if (typeof item != 'undefined'){
		out += '<div class="photo-item" id="div_album'+item.id+'_'+area+'" onmouseover="$(\'#div_album'+item.id+'_'+area+' .actions'+'\').show();" onmouseout="$(\'#div_album'+item.id+'_'+area+' .actions'+'\').hide();">';
			out += (item.img_small ? '<a class="img" href="#" title="'+item.comment+'" onClick="return seePhotoClick('+area+','+id+', true)"><img src="'+item.img_small+'" /></a>' : '');
			out += ((item.isRightEdit || item.is_album_title=='1' || item.isRightDel) ? '<div class="actions">': '');
				out += (item.is_album_title=='1' ? '' : (item.isRightEdit ? '<a href="#" onClick="return albumTitlePhotoClick('+area+','+id+')"><img src="/common/img/ico-default.png" title="'+Dictionary.translate('lang_photostudio_photo_make_title')+'" alt="'+Dictionary.translate('lang_photostudio_photo_make_title_short')+'" /></a>' : ''));
				out += (item.isRightEdit ? '<a href="#" onClick="return editPhotoClick('+area+','+id+')"><img src="/common/img/ico-edit.png" title="'+Dictionary.translate('lang_photostudio_photo_edit')+'" alt="'+Dictionary.translate('lang_photostudio_photo_edit_short')+'" /></a>' : '');
				out += (item.isRightDel ? '<a href="#" onClick="return delPhotoClick('+area+','+id+')"><img src="/common/img/ico-delete.png" title="'+Dictionary.translate('lang_photostudio_photo_delete')+'" alt="'+Dictionary.translate('lang_photostudio_photo_delete_short')+'" /></a>' : '');
			out += ((item.isRightEdit || item.is_album_title=='1' || item.isRightDel) ? '</div>': '');
		out += '</div>';
	}
	return out;
}


function seePhotoClick(area, id, is_new){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		document.location.hash = '#' + id.toString();
		photoCurrent[area] = id;
		$("#id_see_img"+area).attr('src', item.img);
		$("#id_see_comment"+area).text(item.comment);
		$("#id_see_view_counter"+area).text(item.view_counter);
		if ( typeof item.album_url != 'undefined' && item.album_url.length>0 ){
			$("#id_see_toalbum"+area).attr('href',item.album_url);
			$("#id_see_toalbum"+area).show();
		}else{
			$("#id_see_toalbum"+area).hide();
		}
		$("#vk_like_photo"+area).text('');
		viewCounterPhotoClick(area, id);
		if ( item.isRightEdit && item.object=='player' ){
			setAvatarPhotoClick(area, id);
		}
		if ( is_new ){
			$("a[rel='see"+area+"']").click();
		}
		var object = typeof item.object != 'undefined' ? object = item.object : $("#id_see_object"+area).val();
		var object_number = (object=='player' ? 1 : (object=='team' ? 2 : (object=='playfield' ? 3 : 0)));
		VK.Widgets.Like("vk_like_photo"+area, {type: "button"}, id*100+object_number);
	}
	return false;
}

function prevPhotoClick(area){
	var item = photoCurrent[area];
	if (typeof item != 'undefined'){
		for ( var i=0, c=photoArray[area].length; i<c; i++ ){
			if ( photoArray[area][i]==photoCurrent[area] ){
				var num = ( i==0 ? c-1 : i-1 );
				seePhotoClick(area, photoArray[area][num], false);
				break;
			}
		}
	}
	return false;
}

function nextPhotoClick(area){
	var item = photoCurrent[area];
	if (typeof item != 'undefined'){
		for ( var i=0, c=photoArray[area].length; i<c; i++ ){
			if ( photoArray[area][i]==photoCurrent[area] ){
				var num = ( i==c-1 ? 0 : i+1 );
				seePhotoClick(area, photoArray[area][num], false);
				break;
			}
		}
	}
	return false;
}

function editPhotoClick(area, id){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_edit_ID"+area).val(id);
		$("#id_edit_COMMENT"+area).val(item.comment);
		$("a[rel='edit"+area+"']").click();
	}
	return false;
}

function albumTitlePhotoClick(area, id){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_albumTitle_ID"+area).val(id);
		$("a[rel='albumTitle"+area+"']").click();
	}
	return false;
}

function setAvatarPhotoClick(area, id){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_saveAvatar_ID"+area).val(id);
	}
	onClickCancelAvatar(area);
	return false;
}

function viewCounterPhotoClick(area, id){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_viewCounter_object"+area).val(item.object);
		$("#id_viewCounter_ID"+area).val(id);
		$("#id_viewCounter_submit"+area).click();
	}
	return false;
}

function delPhotoClick(area, id){
	var item = photos[area]["rec" + id.toString()];
	if (typeof item != 'undefined'){
		$("#id_del_ID"+area).val(id);
		$("a[rel='del"+area+"']").click();
	}
	return false;
}
